﻿using Assets.Scripts.DL.Consts;
using Assets.Scripts.DL.Mgrs;
using UnityEngine;

public class PlayerKnockDown : PlayerBaseState {
	bool _animStart;
	public PlayerKnockDown(PlayerController controller, PlayerStateEnum stateName, State<PlayerStateEnum> superFSM, StateFactory<PlayerStateEnum> factory) : base(controller, stateName, superFSM, factory) {
		AddConditionToSelf(() => _controller.StateData.IsKnockDown);
	}

	public override void Enter() {
		AudioManager.Play(_controller.StateAudio, AudioStr.KnockDown, false);
		_controller.PlayAnim(PlayerStateEnum.KnockDown);
		_controller.StateData.CanChangeDir = false;
		var v = _controller.ConfigData.KnockDownSpeed;
		var dir = _controller.StateData.FaceDir;
		if (!_controller.StateData.IsKnockFly)
			_controller.Velocity = new UnityEngine.Vector2(-dir * v.x, v.y);
		_animStart = false;
		ScreenEffectMgr.Instance.BrokenEffect();
	}

	public override void Update() {
		base.Update();
		var tmp = true;
		if (_controller.IsAnimPlayOver() && !_animStart && _controller.IsOnGround) {
			_controller.PlayAnim(PlayerStateEnum.StandUp);
			_controller.Velocity = Vector2.zero;
			_animStart = true;
			tmp = false;
		}
		if (_animStart && tmp && _controller.IsAnimPlayOver()) {
			_controller.StateData.IsSpecial = false;
		}
	}

	public override void Exit() {
		_controller.StateData.CanChangeDir = true;
		_controller.StateData.IsKnockDown = false;

	}
}